package com.base.room

import android.content.Context
import androidx.room.*

const val TABLE = "table_test"
const val DB = "test.db"

@Database(entities = [Test::class],version = 1)
abstract class AppDatabase: RoomDatabase(){
    abstract fun getDao(): TestDao

    companion object{
        @Volatile
        private var instance: AppDatabase ?= null
        fun getInstance(context: Context): AppDatabase{
            synchronized(AppDatabase::class){
                if (instance == null){
                    instance = Room.databaseBuilder(context.applicationContext,AppDatabase::class.java, DB)
                        .allowMainThreadQueries()
                        .fallbackToDestructiveMigration()
                        .build()
                }
            }
            return instance!!
        }
    }
}

@Dao
abstract class TestDao(){

    @Insert
    abstract fun insert(test: Test): Long

    @Update
    abstract fun update(test: Test)

    @Query("SELECT * FROM $TABLE")
    abstract fun query(): MutableList<Test>
}


@Entity(tableName = TABLE)
data class Test(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(defaultValue = "0",name = "_key")
    var key: Long
)